package village.rainbow.dailyscene.datamodel;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;

public class DataRow extends LinkedHashMap<String, Object> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -887898965873162152L;
	Hashtable<Integer, Object> mTag;
	
	public DataRow() {
		
	}
	
	public DataRow(HashMap<String, Object> data)
	{
		if (data instanceof DataRow)
			mTag = ((DataRow)data).mTag;
		setData(data);
	}
	public DataRow(DataRow row)
	{
		mTag = row.mTag;
		setData(row);
	}
	public void setData(HashMap<String, Object> data)
	{
		String key = "";
		Iterator<String> iterator = data.keySet().iterator();
		
		while (iterator.hasNext())
		{
			key = iterator.next(); 
			put(key, data.get(key));
		}
	}
	@Override
	public synchronized Object get(Object key) {
		Object o = super.get(key);
		if (o == null)
			return null;

		if((o instanceof String) ==  false)
			if (o.toString().equals("null"))
				return null;

		return o;
	}

	public synchronized Object get(Object key, Object defaultValue) {
		Object o = get(key); 
		if (o == null)		
			return defaultValue; 

		if((o instanceof String) ==  false)
			if (o.toString().equals("null"))
				return defaultValue;

		return o;
	}
	
	
	public synchronized String getString(Object key, String defValue){
		Object o = get(key); 
		
		if(o == null)
			return defValue;
		
		return nvl(o.toString(), defValue);
	}
	public void setTag(String key, Object tag) {
		if(mTag == null) 
			mTag = new Hashtable<Integer, Object>();
		else if(tag == null){
			tag = "";
		}
		mTag.put(key.hashCode(), tag);
	}
	
	public static String nvl(String data, String def) {
		if(data == null) {
			return def;
		}
		return data;
	}
	
	public Object getTag(String key) { 
		if(mTag == null)
			return null;
		
		return mTag.get(key.hashCode());
	}

	public Object getTag(String key, Object defaultValue) { 
		if(mTag == null || !mTag.containsKey(key.hashCode()))
			return defaultValue;
		
		return mTag.get(key.hashCode());
	}
	
	public void setTag(int key, Object tag) {
		if(mTag == null) 
			mTag = new Hashtable<Integer, Object>();
		mTag.put(key, tag);
	}
	
	public Object getTag(int key) { 
		if(mTag == null)
			return null;
		
		return mTag.get(key);
	}
	
	public Object getTag(int key, Object defaultValue) { 
		if(mTag != null && mTag.containsKey(key))
			return mTag.get(key);
		else
			return defaultValue;
	}
	
	public Hashtable<Integer, Object> getTag(){
		if(mTag != null)
			return mTag;
		else
			return new Hashtable<Integer, Object>();
	}
	
	public void clearTags() {
		mTag = null;
	}
	
	@Override
	public Object clone() {
		DataRow dr = new DataRow();
		for(String key : this.keySet()) {
			dr.put(key, this.get(key));
		}
		if (mTag != null)
			dr.mTag = (Hashtable<Integer, Object>)this.mTag.clone();
		return dr;
	}
	public String getString()
	{
		String key = "";
		StringBuilder builder = new StringBuilder(1024);
		Iterator<String> iterator = keySet().iterator();
		
		while (iterator.hasNext())
		{
			key = iterator.next(); 
			builder.append(key + "=" + get(key) + " ");
		}
		
		return builder.toString();
	}

	public static DataRow toDataRow(HashMap<String, String> decodeData) {
		DataRow row = new DataRow();
		for(String key : decodeData.keySet()){
			row.put(key, decodeData.get(key));
		}
		return row;
	}
	
}
